package rursu.dal.managers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import rursu.business.pojo.MoviesList;
import rursu.dal.managers.interfaces.IMoviesListDAL;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MoviesListDALManager implements IMoviesListDAL {

	private SQLiteDatabase db;
	
	public static final String TABLE_NAME = "movies_list";
		
	protected static final String ORDER_BY = "order_by";
	protected static final String DATE = "date";
	protected static final String QUERY = "date";
	protected static final String ID = "_id";			
	
	
	public MoviesListDALManager(SQLiteDatabase db) {
		this.db = db;		
	}
	
	public long insert(MoviesList moviesList) {
		ContentValues initialValues = new ContentValues();

        initialValues.put( MoviesListDALManager.ORDER_BY, moviesList.getOrderBy() );
        initialValues.put( MoviesListDALManager.DATE, moviesList.getDateOfRecord().getTime() );

        return db.insert(TABLE_NAME, null, initialValues);
	}

	public void delete(int id) {
		db.delete(TABLE_NAME, MoviesListDALManager.ID + "=" + id, null);
		
	}

	public MoviesList get(int id) {
		Cursor cursor = db.query(TABLE_NAME, new String [] {
				MoviesListDALManager.ORDER_BY,
				MoviesListDALManager.DATE	},
				MoviesListDALManager.ID + "=" + id, null, null, null, null, "1");

		MoviesList moviesList = null;
		
		if ( cursor.getCount() > 0 ) {
			
			moviesList = new MoviesList();
			
			cursor.moveToFirst();
			
			moviesList.setId(id);
			
			String order = cursor.getString( cursor.getColumnIndex(MoviesListDALManager.ORDER_BY) );
			Date date = new Date( cursor.getLong( cursor.getColumnIndex(MoviesListDALManager.DATE) ) );
			
			moviesList.setOrderBy( order );
			moviesList.setDateOfRecord(date);
			
		}
			
		cursor.close();
		return moviesList;
		
	}

	public List<MoviesList> getAll() {
		
		Cursor cursor = db.query(TABLE_NAME, new String [] {
				MoviesListDALManager.ORDER_BY,
				MoviesListDALManager.ID },
				null, null, null, null, null, null);
		
		List<MoviesList> lists = new ArrayList<MoviesList>();
		
		cursor.moveToFirst();
		
		while ( !cursor.isAfterLast() ) {
			
			MoviesList moviesList = new MoviesList();
			
			String order = cursor.getString( cursor.getColumnIndex(MoviesListDALManager.ORDER_BY) );
			Date date = new Date( cursor.getLong( cursor.getColumnIndex(MoviesListDALManager.DATE) ) );
			
			moviesList.setId( cursor.getInt( cursor.getColumnIndex(MoviesListDALManager.ID) ) );			
			moviesList.setOrderBy( order );
			moviesList.setDateOfRecord(date);
			
			lists.add( moviesList );
			
			cursor.moveToNext();
		}		
		
		cursor.close();
		return (lists.isEmpty()) ? null : lists;
	}

	public boolean update(MoviesList moviesList) {
		// TODO Auto-generated method stub
		return false;
	}

	public MoviesList get(String order) {
		Cursor cursor = db.query(TABLE_NAME, new String [] {
				MoviesListDALManager.ID,
				MoviesListDALManager.DATE },
				MoviesListDALManager.ORDER_BY + "=\"" + order + "\"", null, null, null, null, "1");

		MoviesList moviesList = null;
		
		if (cursor.getCount() > 0) {
			
			moviesList = new MoviesList();
			
			cursor.moveToFirst();
			
			Date date = new Date(cursor.getLong(cursor.getColumnIndex(MoviesListDALManager.DATE)));
			
			moviesList.setId(cursor.getInt(cursor.getColumnIndex(MoviesListDALManager.ID)));
			moviesList.setOrderBy(order);
			moviesList.setDateOfRecord(date);
		}
		
		cursor.close();
		return moviesList;
		
	}

}
